package com.heima.article.job;

import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-22 15:10
 */
@Component
public class HotArticleJob {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleMapper articleMapper;

    @XxlJob("hotArticle")
    public void hotArticle(){
        articleService.hotArticle();
    }

    @XxlJob("hotArticle2")
    public void hotArticle2(){
        //分片广播
        Date date = DateTime.now().minusDays(5).toDate();
        int shardTotal = XxlJobHelper.getShardTotal();  //总分片数
        int shardIndex = XxlJobHelper.getShardIndex(); //当前实例序号
        List<ApArticle> apArticles = articleMapper
                .selectArticlesByDatePartition(date,shardTotal,shardIndex);
        List<Long> collect = apArticles.stream().map(ApArticle::getId).collect(Collectors.toList());
        System.out.println(collect);
    }
}
